<?php

namespace app\stores\controller\admin\cash;

use app\BaseController;
use app\beautiful\model\PrinterModel;
use app\common\model\ProduceDepartModel;
use app\common\model\ProduceTypeModel;
use app\stores\model\cash\PrintRuleModel;
use app\stores\model\cash\PrintSetModel;
use app\stores\model\cash\ReceiptModel;

class ProductionCenter extends BaseController{
    /**
     * 信息
     */
    public function info(){
        $return['cash'] = PrintRuleModel::info(['shop_id'=>$this->request->shopId,'type'=>1,'is_delete'=>0]);
        $return['make'] = PrintRuleModel::info(['shop_id'=>$this->request->shopId,'type'=>2,'is_delete'=>0]);
        $return['chuancai'] = PrintRuleModel::info(['shop_id'=>$this->request->shopId,'type'=>3,'is_delete'=>0]);
        return $this->success($return);
    }
    /**
     * 编辑
     */
    public function save(){
        $type = $this->request->param('type/d',1);
        $receipt_ids = $this->request->param('receipt_ids','');
        $same_name_print = $this->request->param('same_name_print/d',0);
        $not_print_goods_ids = $this->request->param('not_print_goods_ids','');

        if(!in_array($type,[1,2,3])) return $this->error('打印类型不正确');

        $data['receipt_ids'] = trim($receipt_ids,',');
        if($type==1) $data['same_name_print'] = $same_name_print;
        if(in_array($type,[2,3])) $data['not_print_goods_ids'] = trim($not_print_goods_ids,',');
        PrintRuleModel::update($data,['shop_id'=>$this->request->shopId,'type'=>$type,'is_delete'=>0]);
        return $this->success('编辑成功');
    }
    public function default_receipt(){
        $PrintRuleModel= new PrintRuleModel();
        $receipt=$PrintRuleModel::$default_receipt;
        $ReceiptModel = new ReceiptModel();
        $res_recript=[];
        $res_recript['default_1']=$ReceiptModel->where(['type'=>1,'is_effect'=>1])->whereIn('id',$receipt[1])->select()->toArray();
        $res_recript['option_1']=$ReceiptModel->where(['type'=>1,'is_effect'=>1])->whereNotIn('id',$receipt[1])->select()->toArray();
        $cash=PrintRuleModel::info(['shop_id'=>$this->request->shopId,'type'=>1,'is_delete'=>0]);
        if(!empty($cash['receipt_ids'])){
            $cash['receipt_ids']=array_column($cash['receipt_ids'],null,'id');
        }
        foreach ($res_recript['option_1'] as $rk=>$rec){
            $res_recript['option_1'][$rk]['is_selected']=0;
            if(!empty($cash['receipt_ids'][$rec['id']])){
                $res_recript['option_1'][$rk]['is_selected']=1;
            }
        }
        $res_recript['default_2']=$ReceiptModel->where(['type'=>2,'is_effect'=>1])->whereIn('id',$receipt[2])->select()->toArray();
        $res_recript['option_2']=$ReceiptModel->where(['type'=>2,'is_effect'=>1])->whereNotIn('id',$receipt[2])->select()->toArray();
        $make=PrintRuleModel::info(['shop_id'=>$this->request->shopId,'type'=>2,'is_delete'=>0]);
        if(!empty($make['receipt_ids'])){
            $make['receipt_ids']=array_column($make['receipt_ids'],null,'id');
        }
        foreach ($res_recript['option_2'] as $rk=>$rec){
            $res_recript['option_2'][$rk]['is_selected']=0;
            if(!empty($make['receipt_ids'][$rec['id']])){
                $res_recript['option_2'][$rk]['is_selected']=1;
            }
        }
        $res_recript['default_3']=$ReceiptModel->where(['type'=>3,'is_effect'=>1])->whereIn('id',$receipt[3])->select()->toArray();
        $res_recript['option_3']=$ReceiptModel->where(['type'=>3,'is_effect'=>1])->whereNotIn('id',$receipt[3])->select()->toArray();
        $chuancai=PrintRuleModel::info(['shop_id'=>$this->request->shopId,'type'=>3,'is_delete'=>0]);
        if(!empty($chuancai['receipt_ids'])){
            $chuancai['receipt_ids']=array_column($chuancai['receipt_ids'],null,'id');
        }
        foreach ($res_recript['option_3'] as $rk=>$rec){
            $res_recript['option_3'][$rk]['is_selected']=0;
            if(!empty($chuancai['receipt_ids'][$rec['id']])){
                $res_recript['option_3'][$rk]['is_selected']=1;
            }
        }
        return $this->success($res_recript);
    }
    public function printSetList(){
        $shop_id=$this->request->shopId;
        $s=[];
        $s['shop_id']=$shop_id;
        $s['type']=1;
        $s['depart_id']=0;
        $sy_info=PrintSetModel::where($s)->find();
        if(!empty($sy_info)){
            $sy_info=$sy_info->toArray();
            $sy_info['dc_print']=json_decode($sy_info['dc_print'],true);
            $sy_info['dc_print']['print_name']='';
            if(!empty($sy_info['dc_print']['print_id'])){
                $dc_print=PrinterModel::where(['printer_id'=>$sy_info['dc_print']['print_id']])->find();
                if(!empty($dc_print)){
                    $sy_info['dc_print']['print_name']=$dc_print['printer_name'];
                }
            }
            $sy_info['zz_print']=json_decode($sy_info['zz_print'],true);
            $sy_info['zz_print']['print_name']='';
            if(!empty($sy_info['zz_print']['print_id'])){
                $zz_print=PrinterModel::where(['printer_id'=>$sy_info['zz_print']['print_id']])->find();
                if(!empty($zz_print)){
                    $sy_info['zz_print']['print_name']=$zz_print['printer_name'];
                }
            }
            $sy_info['cc_print']=json_decode($sy_info['cc_print'],true);
            $sy_info['cc_print']['print_name']='';
            if(!empty($sy_info['cc_print']['print_id'])){
                $cc_print=PrinterModel::where(['printer_id'=>$sy_info['cc_print']['print_id']])->find();
                if(!empty($cc_print)){
                    $sy_info['cc_print']['print_name']=$cc_print['printer_name'];
                }
            }
            $sy_info['ts_print_name']='';
            $ts_print_id=PrinterModel::where(['printer_id'=>$sy_info['ts_print_id']])->find();
            if(!empty($ts_print_id)){
                $sy_info['ts_print_name']=$ts_print_id['printer_name'];
            }
            $sy_info['wm_print_name']='';
            $wm_print_id=PrinterModel::where(['printer_id'=>$sy_info['wm_print_id']])->find();
            if(!empty($wm_print_id)){
                $sy_info['wm_print_name']=$wm_print_id['printer_name'];
            }
            $sy_info['zt_print_name']='';
            $zt_print_id=PrinterModel::where(['printer_id'=>$sy_info['zt_print_id']])->find();
            if(!empty($zt_print_id)){
                $sy_info['zt_print_name']=$zt_print_id['printer_name'];
            }
            $sy_info['sm_print_name']='';
            $sm_print_id=PrinterModel::where(['printer_id'=>$sy_info['sm_print_id']])->find();
            if(!empty($sm_print_id)){
                $sy_info['sm_print_name']=$sm_print_id['printer_name'];
            }
        }
        $s=[];
        $s['shop_id']=$shop_id;
        $s['origin_type']=2;
        $s['is_effect']=1;
        $s['is_delete']=0;
        $depart_list=ProduceDepartModel::where($s)->field('id,shop_id,name,origin_type,type_id')->select()->toArray();
        if(!empty($depart_list)){
            foreach ($depart_list as &$list){
                $s=[];
                $s['shop_id']=$shop_id;
                $s['type']=2;
                $s['depart_id']=$list['id'];
                $info=PrintSetModel::where($s)->find();
                if(!empty($info)){
                    $info=$info->toArray();
                    $info['dc_print']=json_decode($info['dc_print'],true);
                    $info['dc_print']['print_name']='';
                    if(!empty($info['dc_print']['print_id'])){
                        $dc_print=PrinterModel::where(['printer_id'=>$info['dc_print']['print_id']])->find();
                        if(!empty($dc_print)){
                            $info['dc_print']['print_name']=$dc_print['printer_name'];
                        }
                    }
                    $info['zz_print']=json_decode($info['zz_print'],true);
                    $info['zz_print']['print_name']='';
                    if(!empty($info['zz_print']['print_id'])){
                        $zz_print=PrinterModel::where(['printer_id'=>$info['zz_print']['print_id']])->find();
                        if(!empty($zz_print)){
                            $info['zz_print']['print_name']=$zz_print['printer_name'];
                        }
                    }
                    $info['cc_print']=json_decode($info['cc_print'],true);
                    $info['cc_print']['print_name']='';
                    if(!empty($info['cc_print']['print_id'])){
                        $cc_print=PrinterModel::where(['printer_id'=>$info['cc_print']['print_id']])->find();
                        if(!empty($cc_print)){
                            $info['cc_print']['print_name']=$cc_print['printer_name'];
                        }
                    }
                    $info['ts_print_name']='';
                    $ts_print_id=PrinterModel::where(['printer_id'=>$info['ts_print_id']])->find();
                    if(!empty($ts_print_id)){
                        $info['ts_print_name']=$ts_print_id['printer_name'];
                    }
                    $info['wm_print_name']='';
                    $wm_print_id=PrinterModel::where(['printer_id'=>$info['wm_print_id']])->find();
                    if(!empty($wm_print_id)){
                        $info['wm_print_name']=$wm_print_id['printer_name'];
                    }
                    $info['zt_print_name']='';
                    $zt_print_id=PrinterModel::where(['printer_id'=>$info['zt_print_id']])->find();
                    if(!empty($zt_print_id)){
                        $info['zt_print_name']=$zt_print_id['printer_name'];
                    }
                    $info['sm_print_name']='';
                    $sm_print_id=PrinterModel::where(['printer_id'=>$info['sm_print_id']])->find();
                    if(!empty($sm_print_id)){
                        $info['sm_print_name']=$sm_print_id['printer_name'];
                    }
                }
                $list['print_detail']=$info;
                $list['type_name']='';
                $type_info=ProduceTypeModel::where(['id'=>$list['type_id']])->find();
                if(!empty($type_info)){
                    $list['type_name']=$type_info['name'];
                }
            }
        }
        $res_data['sy_print']=$sy_info;
        $res_data['depart_list']=$depart_list;
        return $this->success($res_data);
    }
    public function printSave(){
        $type = $this->request->param('type/d',1);
        $depart_id = $this->request->param('depart_id/d',0);
        $dc_print = $this->request->param('dc_print',[]);
        $zz_print = $this->request->param('zz_print',[]);
        $cc_print = $this->request->param('cc_print',[]);
        $ts_print_id = $this->request->param('ts_print_id/d',0);
        $ts_num = $this->request->param('ts_num/d',0);
        $wm_print_id = $this->request->param('wm_print_id/d',0);
        $wm_num = $this->request->param('wm_num/d',0);
        $zt_print_id = $this->request->param('zt_print_id/d',0);
        $zt_num = $this->request->param('zt_num/d',0);
        $sm_print_id = $this->request->param('sm_print_id/d',0);
        $sm_num = $this->request->param('sm_num/d',0);
        $shop_id=$this->request->shopId;
        if($type==2 && empty($depart_id)){
            return $this->error('部门id不能为空');
        }
        $insert=[];
        $insert['shop_id']=$shop_id;
        $insert['dc_print']=json_encode($dc_print);
        $insert['zz_print']=json_encode($zz_print);
        $insert['cc_print']=json_encode($cc_print);
        $insert['ts_num']=$ts_num;
        $insert['ts_print_id']=$ts_print_id;
        $insert['wm_print_id']=$wm_print_id;
        $insert['wm_num']=$wm_num;
        $insert['zt_print_id']=$zt_print_id;
        $insert['zt_num']=$zt_num;
        $insert['sm_print_id']=$sm_print_id;
        $insert['sm_num']=$sm_num;
        $insert['type']=$type;
        $insert['depart_id']=$depart_id;
        $s=[];
        $s['shop_id']=$shop_id;
        $s['type']=$type;
        $s['depart_id']=$depart_id;
        $info=PrintSetModel::where($s)->find();
        if(empty($info)){
            $insert['add_time']=date('Y-m-d H:i:s');
            $insert_id=PrintSetModel::insertGetId($insert);
        }else{
            $insert_id=PrintSetModel::up($insert,$s);
        }
        return $this->success('操作成功');
    }
}

